import { Log } from "../../../../../Core/Module/Log/Log";
import { GetTypeOf, Type } from "../../../../../Decorator/Decorator";
import { IMHandler } from "./IMHandler";
import { Session } from "./Session";

export abstract class AMHandler<Message extends Object> extends IMHandler {

    protected abstract Run(session: Session, message: Message);

    public override Handle(session: Session, msg: Object): void {
        let message = msg as Message;
        if (message == null) {
            Log.Error(`消息类型转换错误: {msg.GetType().Name} to {typeof (Message).Name}`);
            return;
        }

        if (session.IsDisposed) {
            Log.Error(`session disconnect ${msg}`);
            return;
        }

        this.Run(session, message).then();
    }

    public override GetMessageType(): Type {
        return GetTypeOf(this._MessageType);
    }

    public override GetResponseType(): Type {
        return null;
    }
}